﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
using Microsoft.Reporting.WebForms;
using System.Data.SqlClient;
using System.Data;


namespace QuanLySanXuat.Controllers
{
    public class PrintMaterialController : Controller
    {
        private readonly IProductModelRepository productModelRepository;
        private readonly IMaterialTypeRepository materialTypeRepository;
        private readonly ICommandBus commandBus;
        private readonly IPrintMaterialRepository printmaterialRepository;
        //
        // GET: /PrintMaterial/
        public PrintMaterialController(ICommandBus commandBus, IProductModelRepository productModelRepository, IMaterialTypeRepository materialTypeRepository, IPrintMaterialRepository printmaterialRepository)
        {
            this.commandBus = commandBus;
            this.productModelRepository = productModelRepository;
            this.materialTypeRepository = materialTypeRepository;
            this.printmaterialRepository = printmaterialRepository;
        }
        public ActionResult Index()
        {
            ViewData["ProductModel"] = productModelRepository.GetAll();
            return View();
        }
        public ActionResult PrintView(int ID)
        {
            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@productModelID",
                Value = ID
            };

            var primaterial = printmaterialRepository.ExecWithStoreProcedure
                                         ("PrintMaterial @productModelID ",
                                            parameter
                                         ).ToList();

            return PartialView("_MaterialList", primaterial);
           
        }
        public FileContentResult Print(int ID)
        {
            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/Report/Reports/RptMaterialType.rdlc");

            var productModel = productModelRepository.GetById(ID);
            ReportParameter p1 = new ReportParameter("NameProduct", productModel.Name);


            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@productModelID",
                Value = ID
            };

            var primaterial = printmaterialRepository.ExecWithStoreProcedure
                                         ("PrintMaterial @productModelID ",
                                            parameter
                                         ).ToList();
            localReport.DataSources.Clear();
            ReportDataSource reportDataSource = new ReportDataSource("DsMaterial", primaterial);
            localReport.SetParameters(new ReportParameter[] { p1 });
            localReport.DataSources.Add(reportDataSource);
            localReport.Refresh();


            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType            
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx            
            string deviceInfo = "<DeviceInfo>" +
                "  <OutputFormat>jpeg</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            //Render the report            
            renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension); 
            return File(renderedBytes, "pdf");
        }
    }
}
